﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfSnqkGasAnalysis.IData;
using WpfSnqkGasAnalysis.Model;


namespace WpfSnqkGasAnalysis.IData_impl
{
    /// <summary>
    /// 气分析，数据接口实现。
    /// </summary>
    /// 创建时间：2022-11-25 13:40:22。作者 ：王浩力
    public sealed class Gas_analysisDAL_impl : BaseDAL, IGas_analysisDAL
    {
        public Result Add(Gas_analysis gas_Analysis)
        {
            Snowflake snowflake = new Snowflake(1, 1);
            long id = 0;
            if (gas_Analysis.Id > 0)
            {
                id = gas_Analysis.Id;
            }
            else
            {
                id = snowflake.NextId();
            }
            string sql = @"INSERT INTO gas_analysis (
                             id,
                             cate,
                             sample_no,
                             well_no,
                             build_name,
                             top_horizon,
                             bottom_horizon,
                             top_well_deep,
                             bottom_well_deep,
                             sampling_location,
                             samping_company_name,
                             analysis_company_name,
                             remark,
                             samping_condition,
                             client,
                             samping_time,
                             analysis_time,
                             samping_user,
                             analysis_user,
                             samping_user_id,analysis_user_id,
                             samping_temperature,
                             oil_pressure,
                             casing_pressure,
                             zong_s,
                             water_point,
                             He,
                             H2,
                             O2,
                             N2,
                             C1,
                             C6,
                             C3,
                             Ic4,
                             Nc4,
                             Ic5,
                             Nc5,
                             CO2,
                             C2,
                             is_desulphurization,
                             H2Smg,
                             all_calc_value,    
                             input_user_id,
                             input_real_name,
                             input_time,
                             o_step
                         )
                         VALUES(
                             " + id + @",
                             $cate,
                             $sample_no,
                             $well_no,
                             $build_name,
                             $top_horizon,
                             $bottom_horizon,
                             $top_well_deep,
                             $bottom_well_deep,
                             $sampling_location,
                             $samping_company_name,
                             $analysis_company_name,
                             $remark,
                             $samping_condition,
                             $client,
                             $samping_time,
                             $analysis_time,
                             $samping_user,
                             $analysis_user,
                              $samping_user_id,$analysis_user_id,
                             $samping_temperature,
                             $oil_pressure,
                             $casing_pressure,
                             $zong_s,
                             $water_point,
                             $He,
                             $H2,
                             $O2,
                             $N2,
                             $C1,
                             $C6,
                             $C3,
                             $Ic4,
                             $Nc4,
                             $Ic5,
                             $Nc5,
                             $CO2,
                             $C2,
                             $is_desulphurization,
                             $H2Smg,
                             $all_calc_value,          
                             $input_user_id,
                             $input_real_name,
                             $input_time,
                             $o_step
                         );
            ";
            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict.Add("$cate", gas_Analysis.Cate);
            dict.Add("$sample_no", gas_Analysis.Sample_no);
            dict.Add("$well_no", gas_Analysis.Well_no);
            dict.Add("$build_name", gas_Analysis.Build_name);
            dict.Add("$top_horizon", gas_Analysis.Top_horizon);
            dict.Add("$bottom_horizon", gas_Analysis.Bottom_horizon);
            dict.Add("$top_well_deep", gas_Analysis.Top_well_deep);
            dict.Add("$bottom_well_deep", gas_Analysis.Bottom_well_deep);
            dict.Add("$sampling_location", gas_Analysis.Sampling_location);
            dict.Add("$samping_company_name", gas_Analysis.Samping_company_name);
            dict.Add("$analysis_company_name", gas_Analysis.Analysis_company_name);
            dict.Add("$remark", gas_Analysis.Remark);
            dict.Add("$samping_condition", gas_Analysis.Samping_condition);
            dict.Add("$client", gas_Analysis.Client);
            dict.Add("$samping_time", gas_Analysis.Samping_time);
            dict.Add("$analysis_time", gas_Analysis.Analysis_time);
            dict.Add("$samping_user", gas_Analysis.Samping_user);
            dict.Add("$analysis_user", gas_Analysis.Analysis_user);

            //添加时间：2023-1-6 17:43:25。作者：王浩力
            dict.Add("$samping_user_id", gas_Analysis.Samping_user_id);
            dict.Add("$analysis_user_id", gas_Analysis.Analysis_user_id);

            dict.Add("$samping_temperature", gas_Analysis.Samping_temperature);
            dict.Add("$oil_pressure", gas_Analysis.Oil_pressure);
            dict.Add("$casing_pressure", gas_Analysis.Casing_pressure);
            dict.Add("$zong_s", gas_Analysis.Zong_s);
            dict.Add("$water_point", gas_Analysis.Water_point);
            dict.Add("$He", gas_Analysis.He);
            dict.Add("$H2", gas_Analysis.H2);
            dict.Add("$O2", gas_Analysis.O2);
            dict.Add("$N2", gas_Analysis.N2);
            dict.Add("$C1", gas_Analysis.C1);
            dict.Add("$C6", gas_Analysis.C6);
            dict.Add("$C3", gas_Analysis.C3);
            dict.Add("$Ic4", gas_Analysis.Ic4);
            dict.Add("$Nc4", gas_Analysis.Nc4);
            dict.Add("$Ic5", gas_Analysis.Ic5);
            dict.Add("$Nc5", gas_Analysis.Nc5);
            dict.Add("$CO2", gas_Analysis.CO2);
            dict.Add("$C2", gas_Analysis.C2);
            dict.Add("$is_desulphurization", gas_Analysis.Is_desulphurization);
            dict.Add("$H2Smg", gas_Analysis.H2Smg);
            dict.Add("$all_calc_value", gas_Analysis.All_calc_value);
            dict.Add("$input_user_id", gas_Analysis.Input_user_id);
            dict.Add("$input_real_name", gas_Analysis.Input_real_name);
            //dict.Add("$input_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
            dict.Add("$input_time", gas_Analysis.Input_time);
            dict.Add("$o_step", gas_Analysis.O_step ?? 1);

            //记录版本数据 gas_analysis_upload_log
            #region 版本数据 gas_analysis_upload_log
            string sql_gas_analysis_upload_log = $@"INSERT INTO gas_analysis_upload_log (
                                        id,
                                        cate,
                                        sample_no,
                                        well_no,
                                        build_name,
                                        top_horizon,
                                        bottom_horizon,
                                        top_well_deep,
                                        bottom_well_deep,
                                        sampling_location,
                                        samping_company_name,
                                        analysis_company_name,
                                        remark,
                                        samping_condition,
                                        client,
                                        samping_time,
                                        analysis_time,
                                        samping_user,
                                        analysis_user,
                                        samping_temperature,
                                        oil_pressure,
                                        casing_pressure,
                                        zong_s,
                                        water_point,
                                        He,
                                        H2,
                                        O2,
                                        N2,
                                        C1,
                                        C6,
                                        C3,
                                        Ic4,
                                        Nc4,
                                        Ic5,
                                        Nc5,
                                        CO2,
                                        C2,
                                        is_desulphurization,
                                        H2Smg,
                                        all_calc_value,
                                        update_time,
                                        update_user,
                                        gas_analysis_key,
                                        data_version
                                    )
                                    VALUES(
                                        {snowflake.NextId()},
                                      $LOG_cate,
$LOG_sample_no,
$LOG_well_no,
$LOG_build_name,
$LOG_top_horizon,
$LOG_bottom_horizon,
$LOG_top_well_deep,
$LOG_bottom_well_deep,
$LOG_sampling_location,
$LOG_samping_company_name,
$LOG_analysis_company_name,
$LOG_remark,
$LOG_samping_condition,
$LOG_client,
$LOG_samping_time,
$LOG_analysis_time,
$LOG_samping_user,
$LOG_analysis_user,
$LOG_samping_temperature,
$LOG_oil_pressure,
$LOG_casing_pressure,
$LOG_zong_s,
$LOG_water_point,
$LOG_He,
$LOG_H2,
$LOG_O2,
$LOG_N2,
$LOG_C1,
$LOG_C6,
$LOG_C3,
$LOG_Ic4,
$LOG_Nc4,
$LOG_Ic5,
$LOG_Nc5,
$LOG_CO2,
$LOG_C2,
$LOG_is_desulphurization,
$LOG_H2Smg,
$LOG_all_calc_value,
$LOG_update_time,
$LOG_update_user,
$LOG_gas_analysis_key,
$LOG_data_version
                                    );            ";

            //记录历史数据
            var mapper = EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<Gas_analysis, Gas_analysis_upload_log>();
            var info = mapper.Map(gas_Analysis);
            info.Gas_analysis_key = gas_Analysis.Id;
            info.data_version = 1;

            dict.Add("$LOG_cate", info.Cate);
            dict.Add("$LOG_sample_no", info.Sample_no);
            dict.Add("$LOG_well_no", info.Well_no);
            dict.Add("$LOG_build_name", info.Build_name);
            dict.Add("$LOG_top_horizon", info.Top_horizon);
            dict.Add("$LOG_bottom_horizon", info.Bottom_horizon);
            dict.Add("$LOG_top_well_deep", info.Top_well_deep);
            dict.Add("$LOG_bottom_well_deep", info.Bottom_well_deep);
            dict.Add("$LOG_sampling_location", info.Sampling_location);
            dict.Add("$LOG_samping_company_name", info.Samping_company_name);
            dict.Add("$LOG_analysis_company_name", info.Analysis_company_name);
            dict.Add("$LOG_remark", info.Remark);
            dict.Add("$LOG_samping_condition", info.Samping_condition);
            dict.Add("$LOG_client", info.Client);
            dict.Add("$LOG_samping_time", info.Samping_time);
            dict.Add("$LOG_analysis_time", info.Analysis_time);
            dict.Add("$LOG_samping_user", info.Samping_user);
            dict.Add("$LOG_analysis_user", info.Analysis_user);
            dict.Add("$LOG_samping_temperature", info.Samping_temperature);
            dict.Add("$LOG_oil_pressure", info.Oil_pressure);
            dict.Add("$LOG_casing_pressure", info.Casing_pressure);
            dict.Add("$LOG_zong_s", info.Zong_s);
            dict.Add("$LOG_water_point", info.Water_point);
            dict.Add("$LOG_He", info.He);
            dict.Add("$LOG_H2", info.H2);
            dict.Add("$LOG_O2", info.O2);
            dict.Add("$LOG_N2", info.N2);
            dict.Add("$LOG_C1", info.C1);
            dict.Add("$LOG_C6", info.C6);
            dict.Add("$LOG_C3", info.C3);
            dict.Add("$LOG_Ic4", info.Ic4);
            dict.Add("$LOG_Nc4", info.Nc4);
            dict.Add("$LOG_Ic5", info.Ic5);
            dict.Add("$LOG_Nc5", info.Nc5);
            dict.Add("$LOG_CO2", info.CO2);
            dict.Add("$LOG_C2", info.C2);
            dict.Add("$LOG_is_desulphurization", info.Is_desulphurization);
            dict.Add("$LOG_H2Smg", info.H2Smg);
            dict.Add("$LOG_all_calc_value", info.All_calc_value);
            dict.Add("$LOG_update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); // info.Update_time);
            dict.Add("$LOG_update_user", info.Update_user);
            dict.Add("$LOG_gas_analysis_key", info.Gas_analysis_key);
            dict.Add("$LOG_data_version", info.data_version);
            #endregion

            string sql_all = sql + sql_gas_analysis_upload_log;
            string msg = null;
            int count = base.ExecSqlCmd(sql_all, dict, cmd =>
          {
              return cmd.ExecuteNonQuery();
          }, exception =>
          {
              //异常消息显示出来
              msg = exception.Message;
          }
          );
            if (count > 0)
            {
                return new Result("新增成功", 200);
            }
            return new Result("新增出错:" + msg);
        }

        /// <summary>
        /// 状态标记。不做物理删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="physicalDelete">是否物理删除？true物理删除</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        /// 实现时间：2022-11-25 14:02:52。作者：王浩力
        public Result Delete(long id, bool physicalDelete = false)
        {
            Result result = new Result("删除出错");
            string sql = $"update  gas_analysis  set  del_time='{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")}'  where  id=" + id;
            if (physicalDelete)
            {
                sql = $"delete  from   gas_analysis    where  id=" + id;
            }
            result = base.ExecSqlCmd<Result>(sql, null, cmd =>
         {
             int count = cmd.ExecuteNonQuery();
             if (count > 0)
             {
                 return new Result(physicalDelete ? "物理删除成功" : "删除成功", 200);
             }
             return new Result("删除出错");
         }, exp =>
         {
             result.Msg = exp.Message;
         });
            return result;
        }

        /// <summary>
        /// 检查编号是否重复了
        /// </summary>
        /// <param name="sampNo"></param>
        /// <returns></returns>
        /// 创建时间：2023-1-10 14:30:50
        public bool CheckNo(string sampNo)
        {
            string sql = "select  count(1)  from   gas_analysis  where  sample_no=$sample_no   ";
            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$sample_no", sampNo);

            return base.ExecSqlCmd(sql, keyValuePairs, cmd =>
            {
                var obj = cmd.ExecuteScalar();
                if (obj.ToMyInt64() > 0)
                {
                    return true;
                }
                return false;
            });
        }

        public Gas_analysis Get(long id)
        {
            string sql = "select *  from   gas_analysis  where  id=" + id;
            return base.ExecSqlCmd(sql, null, cmd =>
           {
               var reader = cmd.ExecuteReader();
               if (!reader.HasRows)
               {
                   return null;
               }
               Gas_analysis ga = new Gas_analysis();
               if (reader.Read())
               {
                   MakeFieldToObject(reader, ga);
               }
               return ga;
           });
        }

        /// <summary>
        /// 字段赋值 
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="ga"></param>
        /// 创建时间：2022-12-7 16:21:21。作者：王浩力
        private static void MakeFieldToObject(System.Data.Common.DbDataReader reader, Gas_analysis ga)
        {
            ga.Id = reader.GetInt64(0);
            ga.Cate = reader["Cate"].ToString();
            ga.Sample_no = reader["sample_no"].ToString();
            ga.Well_no = reader["well_no"].ToString();
            ga.Build_name = reader["build_name"].ToString();
            ga.Top_horizon = reader["top_horizon"].ToString();
            ga.Bottom_horizon = reader["bottom_horizon"].ToString();

            ga.Top_well_deep = reader["top_well_deep"].ToMyDecimal();
            ga.Bottom_well_deep = reader["bottom_well_deep"].ToMyDecimal();

            ga.Sampling_location = reader["sampling_location"].ToString();
            ga.Samping_company_name = reader["samping_company_name"].ToString();
            ga.Analysis_company_name = reader["analysis_company_name"].ToString();
            ga.Remark = reader["remark"].ToString();
            ga.Samping_condition = reader["samping_condition"].ToString();
            ga.Client = reader["client"].ToString();
            ga.Samping_time = reader["samping_time"].ToString();
            ga.Analysis_time = reader["analysis_time"].ToString();

            ga.Samping_user = reader["samping_user"].ToString();
            ga.Analysis_user = reader["analysis_user"].ToString();

            //添加时间：2023-1-6 17:44:59。
            ga.Samping_user_id = reader["Samping_user_id"].ToMyInt64();
            ga.Analysis_user_id = reader["Analysis_user_id"].ToMyInt64();

            ga.Samping_temperature = reader["Samping_temperature"].ToMyDecimal();
            ga.Oil_pressure = reader["oil_pressure"].ToMyDecimal();
            ga.Casing_pressure = reader["casing_pressure"].ToMyDecimal();
            ga.Zong_s = reader["zong_s"].ToMyDecimal();
            ga.Water_point = reader["water_point"].ToMyDecimal();
            ga.He = reader["He"].ToMyDecimal();
            ga.H2 = reader["H2"].ToMyDecimal();
            ga.O2 = reader["O2"].ToMyDecimal();
            ga.N2 = reader["N2"].ToMyDecimal();
            ga.C1 = reader["C1"].ToMyDecimal();
            ga.C6 = reader["C6"].ToMyDecimal();
            ga.C3 = reader["C3"].ToMyDecimal();
            ga.Ic4 = reader["Ic4"].ToMyDecimal();
            ga.Nc4 = reader["Nc4"].ToMyDecimal();
            ga.Ic5 = reader["Ic5"].ToMyDecimal();
            ga.Nc5 = reader["Nc5"].ToMyDecimal();
            ga.CO2 = reader["CO2"].ToMyDecimal();
            ga.C2 = reader["C2"].ToMyDecimal();
            ga.Is_desulphurization = reader["is_desulphurization"] == DBNull.Value ? (byte)0 : Convert.ToByte(reader["is_desulphurization"]);
            ga.H2Smg = reader["H2Smg"].ToMyDecimal();
            ga.All_calc_value = reader["all_calc_value"].ToMyDecimal();

            ga.Proofreader_userId = reader["Proofreader_userId"].ToMyInt64();
            ga.Proofreader = reader["proofreader"].ToString();
            ga.Proofread_time = reader["proofread_time"].ToString();

            ga.Del_time = reader["del_time"].ToString();
            ga.Input_user_id = reader["Input_user_id"] == DBNull.Value ? 0 : Convert.ToInt64(reader["Input_user_id"]);
            ga.Input_real_name = reader["input_real_name"].ToString();
            ga.Input_time = reader["input_time"].ToString();
            ga.O_step = (byte)reader["o_step"].ToString().ToMyDecimal();

            ga.Remark = reader["Remark"].ToString();
        }

        /// <summary>
        /// 气分析。分页
        /// </summary>
        /// <param name="where"></param>
        /// <param name="p"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public PageModel<Gas_analysis> Page(Gas_analysis where, int p, int rows)
        {
            StringBuilder sb = new StringBuilder($@"SELECT id,
       cate,
       sample_no,
       well_no,
       build_name,
     --  top_horizon,
     --  bottom_horizon,
     --  top_well_deep,
     --  bottom_well_deep,
     --  sampling_location,
      samping_company_name,
    --  analysis_company_name,
    --   remark,
    --   samping_condition,
    --  client,
    --  samping_time,
     analysis_time,
    --  samping_user,
    --  analysis_user,
    --  samping_temperature,    
         proofreader,
         proofread_time, 
    --  del_time,
    --   input_user_id,
       input_real_name,
       input_time,
        o_step,
(select check_user_name from gas_check_history where gas_analysis_key=c.id  order by id desc limit 1) as E3    
,(select check_time from gas_check_history where gas_analysis_key=c.id  order by id desc limit 1) as E4   
   
  FROM  gas_analysis AS c   where 1=1   ");

            StringBuilder condictSb = new StringBuilder();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            if (where != null)
            {
                // 大于0返回记录包含删除的记录。
                if (where.E1 <= 0)
                {
                    //不返回被删的
                    condictSb.AppendLine(" and  del_time  is null ");
                }

                //样品编码
                if (!string.IsNullOrWhiteSpace(where.Sample_no))
                {
                    condictSb.AppendLine(" and  sample_no  like  $sample_no ");
                    parameters.Add("$sample_no", "%" + where.Sample_no + "%");
                }
                //开始时间。录入时间
                if (!string.IsNullOrWhiteSpace(where.E3) && DateTime.TryParse(where.E3, out DateTime date11))
                {
                    condictSb.AppendLine(" and  input_time>$date1 ");
                    parameters.Add("$date1", date11.AddDays(-1).ToString("yyyy-MM-dd"));
                }
                //结束时间。录入时间
                if (!string.IsNullOrWhiteSpace(where.E4) && DateTime.TryParse(where.E4, out DateTime date22))
                {
                    condictSb.AppendLine(" and  input_time <$date2 ");
                    parameters.Add("$date2", date22.AddDays(1).ToString("yyyy-MM-dd"));
                }

                //步骤值查询 //添加时间：2022-12-29 16:23:29
                if (where.WorkList?.Count > 0)
                {
                    if (where.WorkList.Count == 1 && where.WorkList[0] != -1)
                    {
                        condictSb.AppendLine($"  and  o_step={where.WorkList[0]}  ");
                    }
                    else
                    {
                        List<string> stepList = new List<string>();
                        foreach (var item in where.WorkList)
                        {
                            stepList.Add($"  o_step={item} ");
                        }
                        string step = $"  and  ({string.Join(" or  ", stepList)})  ";
                        condictSb.AppendLine(step);
                    }
                }

                //类别查询，//添加时间：2023-3-2 09:47:07
                if (!string.IsNullOrWhiteSpace(where.E8))
                {
                    //参数传入格式,多个：1,3   单个：1
                    condictSb.AppendLine($" and  cate in  ({where.E8})  ");
                }
            }
            //总记录数查询语句
            string sqlTotal = "select count(1)  from  gas_analysis  where  1=1  " + condictSb.ToString();

            sb.AppendLine(condictSb.ToString());
            sb.AppendLine($" order by sample_no desc  limit   {rows} offset  {(p - 1) * rows}");
            //客户要以编号为排序，大的在上面 2023-2-10 11:19:39


            List<Gas_analysis> list = base.ExecSqlCmd<List<Gas_analysis>>(sb.ToString(), parameters, cmd =>
            {
                var reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    return null;
                }
                List<Gas_analysis> list = new List<Gas_analysis>();
                while (reader.Read())
                {
                    Gas_analysis ga = new Gas_analysis();
                    ga.Id = reader.GetInt64(0);
                    ga.Cate = reader["Cate"].ToString();
                    ga.Sample_no = reader["sample_no"].ToString();
                    ga.Well_no = reader["well_no"].ToString();
                    ga.Build_name = reader["build_name"].ToString();

                    //ga.Top_horizon = reader["top_horizon"].ToString();
                    //ga.Bottom_horizon = reader["bottom_horizon"].ToString();
                    //ga.Top_well_deep = Convert.ToDecimal(reader["top_well_deep"]);
                    //ga.Bottom_well_deep = Convert.ToDecimal(reader["bottom_well_deep"]);
                    //ga.Sampling_location = reader["sampling_location"].ToString();
                    ga.Samping_company_name = reader["samping_company_name"].ToString();
                    //ga.Analysis_company_name = reader["analysis_company_name"].ToString();
                    //ga.Remark = reader["remark"].ToString();
                    //ga.Samping_condition = reader["samping_condition"].ToString();

                    //ga.Client = reader["client"].ToString();
                    //ga.Samping_time = reader["samping_time"].ToString();
                    ga.Analysis_time = reader["analysis_time"].ToString();
                    //ga.Samping_user = reader["samping_user"].ToString();
                    //ga.Analysis_user = reader["analysis_user"].ToString();

                    ga.Proofreader = reader["proofreader"].ToString();
                    ga.Proofread_time = reader["proofread_time"].ToString();
                    //ga.Del_time = reader["del_time"].ToString();

                    //ga.Input_user_id = reader["Input_user_id"] == DBNull.Value ? 0 : Convert.ToInt64(reader["Input_user_id"]);
                    ga.Input_real_name = reader["input_real_name"].ToString();
                    ga.Input_time = reader["input_time"].ToString();
                    ga.O_step = (byte)reader["o_step"].ToString().ToMyDecimal();
                    ga.E3 = reader["E3"].ToString();//审核人
                    ga.E4 = reader["E4"].ToString();//审核时间  .添加时间：2022-12-21 14:53:50

                    list.Add(ga);
                }
                return list;
            });
            PageModel<Gas_analysis> pageModel = new PageModel<Gas_analysis>()
            {
                PageIndex = p,
                PageSize = rows,
                List = list
            };

            //查询总记录数
            pageModel.Total = base.ExecSqlCmd(sqlTotal, parameters, cmd =>
            {
                var obj = cmd.ExecuteScalar();
                if (obj == null)
                {
                    return 0;
                }
                return (int)obj.ToString().ToMyDecimal();
            });

            return pageModel;
        }

        /// <summary>
        /// 编辑。proofread=true为校对处理
        /// </summary>
        /// <param name="gas_Analysis"></param>
        /// <param name="proofread"> true为校对处理</param>
        /// <returns></returns>
        /// 创建时间：2022-12-8 22:25:38。作者：王浩力
        public IResult Update(Gas_analysis gas_Analysis, bool proofread = false)
        {
            if (gas_Analysis == null || gas_Analysis.Id <= 0)
            {
                return new Result("gas_Analysis不可为空，或Id参数无效");
            }
            var old = Get(gas_Analysis.Id);
            if (old == null)
            {
                return new Result("没有要修改的数据", 200);
            }

            //注释时间：2023-1-10 13:54:38
            ////是否脱硫。
            //gas_Analysis.Is_desulphurization = old.Is_desulphurization;

            //校对sql
            string jiaoduiSQL = "";
            if (proofread)
            {
                jiaoduiSQL = " ,Proofreader_userId=$Proofreader_userId,proofreader=$proofreader,proofread_time=$proofread_time,o_step=$o_step  ";
            }

            string sql = @"UPDATE gas_analysis
   SET
       cate =$cate,
       sample_no =$sample_no,
       well_no =$well_no,
       build_name =$build_name,
       top_horizon =$top_horizon,
       bottom_horizon =$bottom_horizon,
       top_well_deep =$top_well_deep,
       bottom_well_deep =$bottom_well_deep,
       sampling_location =$sampling_location,
       samping_company_name =$samping_company_name,
       analysis_company_name =$analysis_company_name,
       remark =$remark,
       samping_condition =$samping_condition,
       client =$client,
       samping_time =$samping_time,
       analysis_time =$analysis_time,

       samping_user =$samping_user,
       analysis_user =$analysis_user,
samping_user_id =$samping_user_id,
analysis_user_id =$analysis_user_id,

       samping_temperature =$samping_temperature,
       oil_pressure =$oil_pressure,
       casing_pressure =$casing_pressure,
       zong_s =$zong_s,
       water_point =$water_point,
       He =$He,
       H2 =$H2,
       O2 =$O2,
       N2 =$N2,
       C1 =$C1,
       C6 =$C6,
       C3 =$C3,
       Ic4 =$Ic4,
       Nc4 =$Nc4,
       Ic5 =$Ic5,
       Nc5 =$Nc5,
       CO2 =$CO2,
       C2 =$C2,
       is_desulphurization =$is_desulphurization,
       H2Smg =$H2Smg,
       all_calc_value =$all_calc_value " +
      jiaoduiSQL
 + "  WHERE   id =" + gas_Analysis.Id + ";";

            Dictionary<string, object> keyValuePairs = new Dictionary<string, object>();
            keyValuePairs.Add("$cate", gas_Analysis.Cate);
            keyValuePairs.Add("$sample_no", gas_Analysis.Sample_no);
            keyValuePairs.Add("$well_no", gas_Analysis.Well_no);
            keyValuePairs.Add("$build_name", gas_Analysis.Build_name);
            keyValuePairs.Add("$top_horizon", gas_Analysis.Top_horizon);
            keyValuePairs.Add("$bottom_horizon", gas_Analysis.Bottom_horizon);
            keyValuePairs.Add("$top_well_deep", gas_Analysis.Top_well_deep);
            keyValuePairs.Add("$bottom_well_deep", gas_Analysis.Bottom_well_deep);
            keyValuePairs.Add("$sampling_location", gas_Analysis.Sampling_location);
            keyValuePairs.Add("$samping_company_name", gas_Analysis.Samping_company_name);
            keyValuePairs.Add("$analysis_company_name", gas_Analysis.Analysis_company_name);
            keyValuePairs.Add("$remark", gas_Analysis.Remark);
            keyValuePairs.Add("$samping_condition", gas_Analysis.Samping_condition);
            keyValuePairs.Add("$client", gas_Analysis.Client);
            keyValuePairs.Add("$samping_time", gas_Analysis.Samping_time);
            keyValuePairs.Add("$analysis_time", gas_Analysis.Analysis_time);

            keyValuePairs.Add("$samping_user", gas_Analysis.Samping_user);
            keyValuePairs.Add("$analysis_user", gas_Analysis.Analysis_user);

            //添加时间：2023-1-6 17:46:51。
            keyValuePairs.Add("$samping_user_id", gas_Analysis.Samping_user_id);
            keyValuePairs.Add("$analysis_user_id", gas_Analysis.Analysis_user_id);

            keyValuePairs.Add("$samping_temperature", gas_Analysis.Samping_temperature);
            keyValuePairs.Add("$oil_pressure", gas_Analysis.Oil_pressure);
            keyValuePairs.Add("$casing_pressure", gas_Analysis.Casing_pressure);
            keyValuePairs.Add("$zong_s", gas_Analysis.Zong_s);
            keyValuePairs.Add("$water_point", gas_Analysis.Water_point);
            keyValuePairs.Add("$He", gas_Analysis.He);
            keyValuePairs.Add("$H2", gas_Analysis.H2);
            keyValuePairs.Add("$O2", gas_Analysis.O2);
            keyValuePairs.Add("$N2", gas_Analysis.N2);
            keyValuePairs.Add("$C1", gas_Analysis.C1);
            keyValuePairs.Add("$C6", gas_Analysis.C6);
            keyValuePairs.Add("$C3", gas_Analysis.C3);
            keyValuePairs.Add("$Ic4", gas_Analysis.Ic4);
            keyValuePairs.Add("$Nc4", gas_Analysis.Nc4);
            keyValuePairs.Add("$Ic5", gas_Analysis.Ic5);
            keyValuePairs.Add("$Nc5", gas_Analysis.Nc5);
            keyValuePairs.Add("$CO2", gas_Analysis.CO2);
            keyValuePairs.Add("$C2", gas_Analysis.C2);
            keyValuePairs.Add("$is_desulphurization", gas_Analysis.Is_desulphurization);
            keyValuePairs.Add("$H2Smg", gas_Analysis.H2Smg);
            keyValuePairs.Add("$all_calc_value", gas_Analysis.All_calc_value);

            if (proofread)
            {
                keyValuePairs.Add("$Proofreader_userId", gas_Analysis.Proofreader_userId);
                keyValuePairs.Add("$proofreader", gas_Analysis.Proofreader);
                keyValuePairs.Add("$proofread_time", gas_Analysis.Proofread_time);
                keyValuePairs.Add("$o_step", gas_Analysis.O_step);
            }

            //校对修改日志保存
            //检查是否修改了数据，修改了的才记录
            old.Input_time = null;
            old.Input_real_name = null;
            old.Input_user_id = 0;
            old.Proofreader = null;
            old.Proofread_time = null;
            old.Del_time = null;
            old.O_step = null;
            old.Is_desulphurization = 0;
            string json1 = JsonMyHandle.ToJsonStr(old);

            var cp_gas_Analysis = JsonMyHandle.CopyObject<Gas_analysis>(gas_Analysis);
            cp_gas_Analysis.Input_time = null;
            cp_gas_Analysis.Input_real_name = null;
            cp_gas_Analysis.Input_user_id = 0;
            cp_gas_Analysis.Proofreader = null;
            cp_gas_Analysis.Proofread_time = null;
            cp_gas_Analysis.Del_time = null;
            cp_gas_Analysis.O_step = null;
            cp_gas_Analysis.Is_desulphurization = 0;
            string json2 = JsonMyHandle.ToJsonStr(cp_gas_Analysis);
            if (!json1.Equals(json2))
            {
                //新增修改记录
                #region 版本数据 gas_analysis_upload_log
                Snowflake snowflake = new Snowflake(1, 1);
                string sql_gas_analysis_upload_log = $@"INSERT INTO gas_analysis_upload_log (
                                        id,
                                        cate,
                                        sample_no,
                                        well_no,
                                        build_name,
                                        top_horizon,
                                        bottom_horizon,
                                        top_well_deep,
                                        bottom_well_deep,
                                        sampling_location,
                                        samping_company_name,
                                        analysis_company_name,
                                        remark,
                                        samping_condition,
                                        client,
                                        samping_time,
                                        analysis_time,
                                        samping_user,
                                        analysis_user,
                                        samping_temperature,
                                        oil_pressure,
                                        casing_pressure,
                                        zong_s,
                                        water_point,
                                        He,
                                        H2,
                                        O2,
                                        N2,
                                        C1,
                                        C6,
                                        C3,
                                        Ic4,
                                        Nc4,
                                        Ic5,
                                        Nc5,
                                        CO2,
                                        C2,
                                        is_desulphurization,
                                        H2Smg,
                                        all_calc_value,
                                        update_time,
                                        update_user,
                                        gas_analysis_key,
                                        data_version
                                    )
                                    VALUES(
                                        {snowflake.NextId()},
                                      $LOG_cate,
$LOG_sample_no,
$LOG_well_no,
$LOG_build_name,
$LOG_top_horizon,
$LOG_bottom_horizon,
$LOG_top_well_deep,
$LOG_bottom_well_deep,
$LOG_sampling_location,
$LOG_samping_company_name,
$LOG_analysis_company_name,
$LOG_remark,
$LOG_samping_condition,
$LOG_client,
$LOG_samping_time,
$LOG_analysis_time,
$LOG_samping_user,
$LOG_analysis_user,
$LOG_samping_temperature,
$LOG_oil_pressure,
$LOG_casing_pressure,
$LOG_zong_s,
$LOG_water_point,
$LOG_He,
$LOG_H2,
$LOG_O2,
$LOG_N2,
$LOG_C1,
$LOG_C6,
$LOG_C3,
$LOG_Ic4,
$LOG_Nc4,
$LOG_Ic5,
$LOG_Nc5,
$LOG_CO2,
$LOG_C2,
$LOG_is_desulphurization,
$LOG_H2Smg,
$LOG_all_calc_value,
$LOG_update_time,
$LOG_update_user,
$LOG_gas_analysis_key,
$LOG_data_version
                                    );            ";

                //记录历史数据
                var mapper = EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<Gas_analysis, Gas_analysis_upload_log>();
                var info = mapper.Map(gas_Analysis);
                info.Gas_analysis_key = gas_Analysis.Id;
                info.data_version = 2;

                keyValuePairs.Add("$LOG_cate", info.Cate);
                keyValuePairs.Add("$LOG_sample_no", info.Sample_no);
                keyValuePairs.Add("$LOG_well_no", info.Well_no);
                keyValuePairs.Add("$LOG_build_name", info.Build_name);
                keyValuePairs.Add("$LOG_top_horizon", info.Top_horizon);
                keyValuePairs.Add("$LOG_bottom_horizon", info.Bottom_horizon);
                keyValuePairs.Add("$LOG_top_well_deep", info.Top_well_deep);
                keyValuePairs.Add("$LOG_bottom_well_deep", info.Bottom_well_deep);
                keyValuePairs.Add("$LOG_sampling_location", info.Sampling_location);
                keyValuePairs.Add("$LOG_samping_company_name", info.Samping_company_name);
                keyValuePairs.Add("$LOG_analysis_company_name", info.Analysis_company_name);
                keyValuePairs.Add("$LOG_remark", info.Remark);
                keyValuePairs.Add("$LOG_samping_condition", info.Samping_condition);
                keyValuePairs.Add("$LOG_client", info.Client);
                keyValuePairs.Add("$LOG_samping_time", info.Samping_time);
                keyValuePairs.Add("$LOG_analysis_time", info.Analysis_time);
                keyValuePairs.Add("$LOG_samping_user", info.Samping_user);
                keyValuePairs.Add("$LOG_analysis_user", info.Analysis_user);
                keyValuePairs.Add("$LOG_samping_temperature", info.Samping_temperature);
                keyValuePairs.Add("$LOG_oil_pressure", info.Oil_pressure);
                keyValuePairs.Add("$LOG_casing_pressure", info.Casing_pressure);
                keyValuePairs.Add("$LOG_zong_s", info.Zong_s);
                keyValuePairs.Add("$LOG_water_point", info.Water_point);
                keyValuePairs.Add("$LOG_He", info.He);
                keyValuePairs.Add("$LOG_H2", info.H2);
                keyValuePairs.Add("$LOG_O2", info.O2);
                keyValuePairs.Add("$LOG_N2", info.N2);
                keyValuePairs.Add("$LOG_C1", info.C1);
                keyValuePairs.Add("$LOG_C6", info.C6);
                keyValuePairs.Add("$LOG_C3", info.C3);
                keyValuePairs.Add("$LOG_Ic4", info.Ic4);
                keyValuePairs.Add("$LOG_Nc4", info.Nc4);
                keyValuePairs.Add("$LOG_Ic5", info.Ic5);
                keyValuePairs.Add("$LOG_Nc5", info.Nc5);
                keyValuePairs.Add("$LOG_CO2", info.CO2);
                keyValuePairs.Add("$LOG_C2", info.C2);
                keyValuePairs.Add("$LOG_is_desulphurization", info.Is_desulphurization);
                keyValuePairs.Add("$LOG_H2Smg", info.H2Smg);
                keyValuePairs.Add("$LOG_all_calc_value", info.All_calc_value);
                keyValuePairs.Add("$LOG_update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); // info.Update_time);
                keyValuePairs.Add("$LOG_update_user", info.Update_user);
                keyValuePairs.Add("$LOG_gas_analysis_key", info.Gas_analysis_key);
                keyValuePairs.Add("$LOG_data_version", info.data_version);

                sql += sql_gas_analysis_upload_log;
                #endregion
            }

            string msg = string.Empty;
            int count = base.ExecSqlCmd(sql, keyValuePairs, cmd =>
          {
              return cmd.ExecuteNonQuery();
          }, exp =>
          {
              msg = exp.Message;
          }
          );
            return count > 0 ? new ResultSuccess("修改成功") : new Result("修改失败，" + msg);
        }

        public IResult UpdateOstep(byte oStep, long id)
        {
            string sql = $"update  Gas_analysis  set  o_step={oStep}   where  id={id} ";
            int count = base.ExecSqlCmd(sql, null, c =>
             {
                 return c.ExecuteNonQuery();
             });
            if (count > 0)
            {
                return new ResultSuccess("修改成功");
            }
            return new ResultSuccess("操作失败");
        }

        /// <summary>
        /// 统计有多少个井，返回井列表
        /// </summary>
        /// <returns></returns>
        /// 创建时间：2023-1-18 09:41:42。作者：王浩力
        public List<BaseModel> GetWellNoList()
        {
            string sql = @" SELECT   distinct       well_no,length(well_no) as s  
  FROM gas_analysis  where  s>0 ; ";
            return base.ExecSqlCmd(sql, null, c =>
            {
                var reader = c.ExecuteReader();

                if (!reader.HasRows)
                {
                    return null;
                }
                List<BaseModel> list = new List<BaseModel>();
                while (reader.Read())
                {
                    BaseModel info = new BaseModel();
                    info.E3 = reader["well_no"].ToString();
                    list.Add(info);
                }
                return list;
            });
        }

        /// <summary>
        /// 指标统计，最大返回2万个点
        /// </summary>
        /// <param name="zb">指标</param>
        /// <param name="cate">样品类别</param>
        /// <param name="dateStart">开始日期</param>
        /// <param name="dateEnd">结束日期</param>
        /// <returns></returns>
        /// 创建时间：2022-12-30 18:03:59。作者：王浩力
        public List<PointView> GetPointCollection(string zb, string cate, string dateStart, string dateEnd, string well)
        {
            string sql = $@"  SELECT  {zb}     
  FROM  gas_analysis
where analysis_time>= $dateStart  and  analysis_time<= $dateEnd 
and cate = $cate
and  well_no=$well_no
and del_time is null
order by id asc   limit   20000   ";
            Dictionary<string, object> kv = new Dictionary<string, object>();
            kv.Add("$dateStart", Convert.ToDateTime(dateStart).ToString("yyyy-MM-dd"));
            kv.Add("$dateEnd", Convert.ToDateTime(dateEnd).ToString("yyyy-MM-dd"));
            kv.Add("$cate", cate);
            kv.Add("$well_no", well);

            return ExecSqlCmd(sql, kv, c =>
             {
                 var reader = c.ExecuteReader();
                 if (!reader.HasRows)
                 {
                     return null;
                 }
                 List<PointView> pointViews = new List<PointView>();
                 while (reader.Read())
                 {
                     double km = (double)reader[zb].ToMyDecimal();
                     pointViews.Add(new PointView() { Y = km });
                 }
                 return pointViews;
             });

        }

        /// <summary>
        /// 根据样本类别统计样本数量，审核通过的，没有被删除的
        /// </summary>
        /// <param name="cate">样本类别</param>
        /// 创建时间：2023-1-3 09:15:58。作者：王浩力
        public int GetCateCount(string cate)
        {
            string sql = @$" SELECT  count(1)
                        FROM gas_analysis where cate = $cate and o_step = 4 and del_time is null ";
            Dictionary<string, object> kv = new Dictionary<string, object>();
            kv.Add("$cate", cate);
            return ExecSqlCmd(sql, kv, c =>
            {
                long va = (long)c.ExecuteScalar().ToMyInt64();
                return (int)va;
            });
        }

        /// <summary>
        /// 根据样本类别统计样本数量，审核通过的，没有被删除的
        /// </summary>
        /// 创建时间：2023-1-3 10:19:34。作者：王浩力
        public int GetOtherCateCount()
        {
            string sql = @$"SELECT  count(1)
  FROM  gas_analysis  where cate<>'1' and cate<>'3' and cate<>'4'   
   and o_step=4 and del_time is null  ";
            return ExecSqlCmd(sql, null, c =>
            {
                long va = (long)c.ExecuteScalar().ToMyInt64();
                return (int)va;
            });
        }

    }
}
